package com.syqc.monitor.ui.alarm

import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.syqc.comlib.utils.toast
import com.syqc.entity.AlarmNote
import com.syqc.monitor.R
import com.syqc.monitor.databinding.IncludeSelectSureBinding
import com.syqc.monitor.databinding.PopupPhraseBinding

/**
 *@Author Jasper
 *@Time 2021/9/27 17:28
 *@Desc
 */
class AlarmTextPopup(private val context: Context) : PopupWindow() {

    private val binding = PopupPhraseBinding.inflate(LayoutInflater.from(context))
    private var bind: IncludeSelectSureBinding
    private val mDefineAdapter by lazy { DescAdapter() }
    private val mNormalAdapter by lazy { DescAdapter() }
    private var desc: String = ""

    init {
        contentView = binding.root
        binding.recyclerDefine.layoutManager = LinearLayoutManager(context)
        binding.recyclerDefine.adapter = mDefineAdapter
        binding.recyclerNormal.layoutManager = LinearLayoutManager(context)
        binding.recyclerNormal.adapter = mNormalAdapter
        bind = IncludeSelectSureBinding.bind(binding.root)
        bind.tvSure.setText(R.string.desc_com_sure)

        isFocusable = true //popupwindow设置焦点
        setBackgroundDrawable(ColorDrawable(-0x56000000)) //设置背景
//        isOutsideTouchable = true //点击外面窗口消失
        width = LinearLayout.LayoutParams.MATCH_PARENT //设置SelectPicPopupWindow弹出窗体的宽
        height = LinearLayout.LayoutParams.MATCH_PARENT //设置SelectPicPopupWindow弹出窗体的高
        setListener()
        contentView.findViewById<ImageView>(R.id.ivPopupClose).setOnClickListener { dismiss() }
        isClippingEnabled = false
    }

    fun setListener() {
        binding.tvDefine.setOnClickListener {
            binding.tvDefine.isSelected = true
            binding.tvNormal.isSelected = false
            binding.recyclerDefine.isVisible = true
            binding.recyclerNormal.isVisible = false
        }

        binding.tvNormal.setOnClickListener {
            binding.tvDefine.isSelected = false
            binding.tvNormal.isSelected = true
            binding.recyclerDefine.isVisible = false
            binding.recyclerNormal.isVisible = true
        }

        binding.tvDefine.isSelected = true
        binding.tvNormal.isSelected = false

        mDefineAdapter.setOnItemClickListener { adapter, view, position ->
            mDefineAdapter.index = position
            desc = mDefineAdapter.data[position]
            adapter.notifyDataSetChanged()
            mNormalAdapter.index = -1
            mNormalAdapter.notifyDataSetChanged()
        }
        mNormalAdapter.setOnItemClickListener { adapter, view, position ->
            mNormalAdapter.index = position
            desc = mNormalAdapter.data[position]
            adapter.notifyDataSetChanged()
            mDefineAdapter.index = -1
            mDefineAdapter.notifyDataSetChanged()
        }
        binding.ivPopupClose.setOnClickListener {
            dismiss()
        }
        bind.tvSure.setOnClickListener {
            if (desc.isEmpty()) {
                context.toast(R.string.toast_choose_text)
                return@setOnClickListener
            }
            dismiss()
            mCallback?.callback(desc)
        }
    }

    fun showAtLocation(parent: View?, entity: AlarmNote) {
        showAtLocation(parent, Gravity.NO_GRAVITY, 0, 0)
        mNormalAdapter.index = -1
        mDefineAdapter.index = -1
        if (entity.userNote.isNullOrEmpty()) {
            binding.tvDefine.isVisible = false
            binding.recyclerDefine.isVisible = false
            binding.recyclerNormal.isVisible = true
            binding.tvNormal.isSelected = true
            binding.tvDefine.isSelected = false
        } else {
            binding.tvDefine.isVisible = true
            binding.recyclerNormal.isVisible = false
            binding.recyclerDefine.isVisible = true
            binding.tvDefine.isSelected = true
            binding.tvNormal.isSelected = false
            mDefineAdapter.setList(entity.userNote)
        }
        mNormalAdapter.setList(entity.sysNote)
        desc = ""
    }

    private var mCallback: OnCallback? = null

    fun setOnCallback(callback: OnCallback) {
        mCallback = callback
    }

    interface OnCallback {
        fun callback(desc: String)
    }

    inner class DescAdapter : BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_phrase) {
        var index = -1
        override fun convert(holder: BaseViewHolder, item: String) {
            val tvLoginItemTitle = holder.getView<TextView>(R.id.tvLoginItemTitle)
            tvLoginItemTitle.text = item
            tvLoginItemTitle.isSelected = holder.layoutPosition == index
        }
    }

}